package com.android.tvremoteplay;
 
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
 
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
 
/**
 * Created by chenpengfei_d on 2016/9/1.
 */
public class DkToast {
    private static Toast mToast;
    private static Context mContext;
    private static TextView mTextView;
    private static long duration;
    private static DkToast mDkToast;
    private static final int SHOW = 1;
    private static final int HIDE = 0;
    private static Object mTN;
    private static Method mShow;
    private static Method mHide;
    private static Field mViewFeild;
    private static Handler handler  = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
 
                case SHOW:
                    handler.sendEmptyMessageDelayed(HIDE,duration);
                    break;
 
                case HIDE:
                    hide();
                    break;
            }
        }
    };
    public static DkToast makeDkToast(Context context,String msg,long dur) {
        if (mToast!=null){
            mToast.cancel();
        }
        mDkToast = new DkToast();
        mContext = context;
        duration = dur;
        mToast = new Toast(mContext);
        mTextView = new TextView(mContext);
        mTextView.setText(msg);
        mTextView.setTextSize(18);
        mTextView.setTextColor(Color.RED);
        mToast.setView(mTextView);
        mToast.setGravity(Gravity.CENTER,0,0);
        reflectToast();
        return mDkToast;
    }
    public  static DkToast makeDkToast(Context context, String text,String text2) {
        if (mToast!=null){
            mToast.cancel();
        }
        mContext = context;
        duration = 15000;
        mToast = new Toast(mContext);
        mDkToast = new DkToast();
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.toast, null);
        ImageView imageView=(ImageView)view.findViewById(R.id.toast_image);
        if (text2.equals("高温预警")){
            imageView.setBackgroundResource(R.drawable.test);
        }
        if (text2.equals("暴雨预警")){
            imageView.setBackgroundResource(R.drawable.test2);
        }
        TextView t = (TextView) view.findViewById(R.id.toast_text);
        t.setText(text);
        mToast.setView(view);
        mToast.setGravity(Gravity.CENTER,000,0);
        reflectToast();
        return mDkToast;
    }

 
    public static void reflectToast(){
        Field field = null;
        try {
            field = mToast.getClass().getDeclaredField("mTN");
            field.setAccessible(true);
            mTN = field.get(mToast);
            mShow = mTN.getClass().getDeclaredMethod("show");
            mHide = mTN.getClass().getDeclaredMethod("hide");
            mViewFeild = mTN.getClass().getDeclaredField("mNextView");
            mViewFeild.setAccessible(true);
 
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e1) {
            e1.printStackTrace();
        }
 
    }
    public static void show(){
 
        try {
            //android4.0以上就要以下处理
            if(Build.VERSION.SDK_INT >14) {
                Field mNextViewField = mTN.getClass().getDeclaredField("mNextView");
                mNextViewField.setAccessible(true);
                LayoutInflater inflate = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                View v = mToast.getView();
                mNextViewField.set(mTN, v);
                Method method = mTN.getClass().getDeclaredMethod("show", null);
                method.invoke(mTN, null);
            }
            mShow.invoke(mTN, null);
        }catch (Exception e){
            e.printStackTrace();
        }
 
        handler.sendEmptyMessage(SHOW);
    }
 
    private static void hide(){
        try {
            mHide.invoke(mTN, null);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }
}
